﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace lgp1985._3DVisionReady
{
    public partial class _3DVisionReady : ServiceBase
    {
        public _3DVisionReady()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Watcher_EventArrived(null, null);
            var watcher = new ManagementEventWatcher("SELECT * FROM RegistryValueChangeEvent WHERE Hive = 'HKEY_LOCAL_MACHINE'" +
                @"AND KeyPath = 'SOFTWARE\\Wow6432Node\\NVIDIA Corporation\\Global\\Stereo3D' AND ValueName='StereoViewerType'");
            watcher.EventArrived += Watcher_EventArrived;
            watcher.Start();
        }

        private void Watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            const string regKeyPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\NVIDIA Corporation\Global\Stereo3D\";
            const string regValueName = "StereoViewerType";
            const Int32 expectedValue = 1;
            if ((Int32)Registry.GetValue(regKeyPath, regValueName, -1) != expectedValue)
            {
                Registry.SetValue(regKeyPath, regValueName, expectedValue);
                EventLog.WriteEntry("Redefined to 3D Vision Ready", EventLogEntryType.Information);
            }
        }
    }
}